package com.thread;

import java.util.Calendar;
import java.util.TimerTask;
import java.util.concurrent.ScheduledFuture;

import com.Util.GeneralThreadPool;


public class TimeCheckThread  extends TimerTask {


	@Override
	public void run() {
		try{
			if(GetNowHour() == 5 || GetNowHour() == 17){
				if(GetNowMinute() >=59){
					Process process = Runtime.getRuntime().exec("shutdown /r /t 60");
					try {
						process.waitFor();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					GeneralThreadPool.getInstance().cancel(this._timer, false);
					System.exit(0);
				}
			}
		}catch(Exception e){
			GeneralThreadPool.getInstance().cancel(this._timer, false);
			this.start();
		}
	}
	private ScheduledFuture<?> _timer;
	
	public void start() {
		final int timeMillis = 1000;// 1秒
		this._timer = GeneralThreadPool.getInstance().scheduleAtFixedRate(this, timeMillis, timeMillis);
	}
	
	

	public static int GetNowHour() {//24小時制
		final Calendar rightNow = Calendar.getInstance();//TODO 取得預設月曆物件
		int nowHour;
		nowHour = rightNow.get(Calendar.HOUR_OF_DAY);//TODO 取得此時之值
		return nowHour;//TODO 傳回取得此時之值
	}

	public static int GetNowMinute() {
		final Calendar rightNow = Calendar.getInstance();//TODO 取得預設月曆物件
		int nowMinute;
		nowMinute = rightNow.get(Calendar.MINUTE);//TODO 取得此分之值
		return nowMinute;//TODO 傳回取得此分之值
	}




}
